package org.wak.action.sci
{
	import org.wak.action.ActionsFactory;
	import org.wak.action.IActionController;
	import org.wak.action.IInteractable;
	import org.wak.action.actions.AdventureActions;
	import org.wak.action.actions.BaseAction;
	import org.wak.character.AdventureCharacter;
	import org.wak.gui.events.GuiEvent;
	import org.wak.gui.sci.SciGui;
	import org.wak.main.AdventureMaster;
	import org.wak.main.PuzzleControllerBase;
	import org.wak.scene.events.SceneEvent;
	import org.wak.utils.IIterator;

	/**
	 * Action controller for the SCI style. 
	 * 
	 */
	public class ActionControllerSCI implements IActionController
	{
		protected var mPlayerChar:AdventureCharacter = null ; 
		protected var mActiveAction:BaseAction = null ;
		protected var mSciGui:SciGui = null ; 
		
		
		/**
		 * Constructor. 
		 * 
		 */		
		public function ActionControllerSCI()
		{
		}
		
		/** @inheritDoc	 */		
		public function Initialize():void
		{
			mPlayerChar = AdventureMaster.CharactersMngr.GetCharacter( AdventureMaster.defaultPlayerCharacterId );
			mActiveAction = ActionsFactory.CreateAction( AdventureActions.ActionIdWalkTo );
			mSciGui = AdventureMaster.GuiMngr.adventureGui as SciGui ;
			mSciGui.SetMouseIconAction( AdventureActions.ActionIdWalkTo );
			
		}
		
		/** @inheritDoc	 */
		public function OnSceneItemClick(event:SceneEvent):void
		{
			//trace("SCI controller OnSceneItemClick");
			// if the player's clicks on his avatar, just ignore it
			// and walk to that scene spot.
			if( (event.interactable == mPlayerChar.characterDisplay) || (event.interactable == null) )
			{
				if( mActiveAction.GetId() == AdventureActions.ActionIdWalkTo)
				{
					mPlayerChar.MoveTo( event.sceneMouseX, event.sceneMouseY);
					
					//mActiveAction = ActionsFactory.CreateAction( AdventureActions.ActionIdWalkTo );
					//mScummGui.SetFeedbackText( mActiveAction.GetText() );
					return ;
				}
			}
			else
			{
				if( mActiveAction.GetId() == AdventureActions.ActionIdWalkTo)
				{
					mPlayerChar.MoveTo( event.interactable.interactionPointX, event.interactable.interactionPointY);
					return ;
				}
			}
			
			// if the active action is ready, this ends here
			if(mActiveAction.isReady )
				return ;
			
 			mActiveAction.AddParameter(event.interactable);
			
			if( mActiveAction.isReady )
			{
				var fMethodFound:Boolean = mActiveAction.CanExecute( AdventureMaster.ActionsMngr.GetPuzzleControllers() );
				// If the method can execute
				if( fMethodFound )
				{
					//trace("method found");
					mActiveAction.Execute( AdventureMaster.ActionsMngr.GetPuzzleControllers() );
					
				}
				else
				{
					//trace("NO  method found");
					AdventureMaster.DialogMngr.DisplayInfo(mActiveAction.GetNoActionText());
					
				}
				var fParam:IInteractable = mActiveAction.GetParam1() ;
				// Reset the active action.
				mActiveAction = ActionsFactory.CreateAction(mActiveAction.GetId());
				if( mActiveAction.paramsNumber == 2 )
					mActiveAction.AddParameter( fParam );
			}
		}
		
		/** @inheritDoc	 */
		public function OnSceneItemMouseOver(event:SceneEvent):void
		{
		}
		
		/** @inheritDoc	 */
		public function OnSceneItemMouseOut(event:SceneEvent):void
		{
		}
		
		/** @inheritDoc	 */
		public function OnSceneSpaceClick(event:SceneEvent):void
		{
			if( mActiveAction.GetId() == AdventureActions.ActionIdWalkTo)
			{
				mPlayerChar.MoveTo( event.sceneMouseX,event.sceneMouseY);
				//mActiveAction = ActionsFactory.CreateAction( AdventureActions.ActionIdWalkTo );
			}
		}
		
		/**
		 * @inheritDoc
		 */		
		public function OnEnterScene(event:SceneEvent):void
		{
			mActiveAction = ActionsFactory.CreateAction( AdventureActions.ActionIdWalkTo );
			
			var fMethodName:String = "EnterScene"+event.sceneId ;
			
			var fPIt:IIterator = AdventureMaster.ActionsMngr.GetPuzzleControllers();
			
			
			var fMethodFound:Boolean = false ;
			var fPuz:PuzzleControllerBase = null ;
			while( fPIt.hasNext() )
			{
				fPuz = fPIt.next() as PuzzleControllerBase ;
				fMethodFound = fPuz.hasOwnProperty(fMethodName);
				if(fMethodFound)
				{
					fPuz[fMethodName](event.sceneId) ;
				}
			}
		}
		
		/**
		 * @inheritDoc
		 */	
		public function OnExitScene(event:SceneEvent):void
		{
			trace("Modern Actioncontroller OnExitScene");
			
			var fMethodName:String = "ExitScene"+event.sceneId ;
			
			var fPIt:IIterator = AdventureMaster.ActionsMngr.GetPuzzleControllers();
			
			
			var fMethodFound:Boolean = false ;
			var fPuz:PuzzleControllerBase = null ;
			while( fPIt.hasNext() )
			{
				fPuz = fPIt.next() as PuzzleControllerBase ;
				fMethodFound = fPuz.hasOwnProperty(fMethodName);
				if(fMethodFound)
				{
					fPuz[fMethodName](event.sceneId) ;
				}
			}
		}
		
		/** @inheritDoc	 */
		public function OnActionSelected(event:GuiEvent):void
		{
			//trace("SCI controller OnActionSelected:"+event.buttonId)
			
					
			mActiveAction = ActionsFactory.CreateAction( event.buttonId );
			if( mActiveAction.GetId() == AdventureActions.ActionIdUse )
			{
				mActiveAction.AddParameter( event.inventoryItem );
			}
		}
		
		/** @inheritDoc	 */
		public function OnInventoryItemSelected(event:GuiEvent):void
		{
			if(mActiveAction.isReady )
				return ;
			
 			mActiveAction.AddParameter(event.inventoryItem);
			
			if( mActiveAction.isReady )
			{
				var fMethodFound:Boolean = mActiveAction.CanExecute( AdventureMaster.ActionsMngr.GetPuzzleControllers() );
				// If the method can execute
				if( fMethodFound )
				{
					//trace("method found");
					mActiveAction.Execute( AdventureMaster.ActionsMngr.GetPuzzleControllers() );
					
				}
				else
				{
					//trace("NO  method found");
					AdventureMaster.DialogMngr.DisplayInfo(mActiveAction.GetNoActionText());
					
				}
				var fParam:IInteractable = mActiveAction.GetParam1() ;
				// Reset the active action.
				mActiveAction = ActionsFactory.CreateAction(mActiveAction.GetId());
				if( mActiveAction.paramsNumber == 2 )
					mActiveAction.AddParameter( fParam );
			}
		}
		
		/** @inheritDoc	 */
		public function OnInventoryItemOver(event:GuiEvent):void
		{
		}
		
		/** @inheritDoc	 */
		public function OnInventoryItemOut(event:GuiEvent):void
		{
		}
		
		/** @inheritDoc	 */
		public function EndOfAction():void
		{
		}
		
	}
}